package com.nandi.emergencyinspection.ui.activity

import android.content.Intent
import android.os.Bundle
import com.luck.picture.lib.tools.ToastUtils
import com.nandi.emergencyinspection.R
import com.nandi.emergencyinspection.bean.EditUserBean
import com.nandi.emergencyinspection.mvp.PwdContract
import com.nandi.emergencyinspection.mvp.PwdPresenter

import com.tbright.ktbaselibrary.mvp.BaseMvpActivity
import com.tbright.ktbaselibrary.utils.ActivityUtils
import kotlinx.android.synthetic.main.activity_pwd.*
import kotlinx.android.synthetic.main.header_layout.*

class PwdActivity : BaseMvpActivity<PwdPresenter>(), PwdContract.PwdView {
    override fun getLayoutId(): Int {
        return R.layout.activity_pwd
    }

    override fun initView(savedInstanceState: Bundle?) {
        left_icon.setOnClickListener {
            finish()
        }
        center_txt.text = "修改密码"
        submit.setOnClickListener {
            if (verification()) {
                val editUserBean = EditUserBean()
                editUserBean.password = getOldPwd()
                editUserBean.newpassword1 = getNewPwd()
                mPresenter?.editInfoAsync(editUserBean)
            } else {
                ToastUtils.s(this, "请检查填写的信息")
            }
        }

    }

    private fun verification(): Boolean {
        if (getNewPwd() != getTooPwd()) {
            ToastUtils.s(this, "两次密码输入不一致")
            return false
        }
        return getOldPwd().isNotEmpty() && getNewPwd().isNotEmpty() && getTooPwd().isNotEmpty()
    }

    private fun getOldPwd(): String {
        return old_pwd.text.toString().trim()
    }

    private fun getNewPwd(): String {
        return new_pwd.text.toString().trim()
    }

    private fun getTooPwd(): String {
        return too_pwd.text.toString().trim()
    }

    override fun showEditInfoAsyncResult(result: String) {
        ActivityUtils.finishAllActivities()
        startActivity(Intent(this, LoginActivity::class.java))
    }
}